package com.woniuxy.yoga.course.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.coach.dao.model.Coach;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.CourseState;
import com.woniuxy.yoga.commons.core.statedata.ProductState;
import com.woniuxy.yoga.commons.core.util.DateUtil;
import com.woniuxy.yoga.course.client.*;
import com.woniuxy.yoga.course.client.dto.course.CourseTypeDTO;
import com.woniuxy.yoga.course.client.dto.product.ProductTypeByIdDTO;
import com.woniuxy.yoga.course.client.dto.progress.Expcard;
import com.woniuxy.yoga.course.client.dto.progress.Lifecard;
import com.woniuxy.yoga.course.client.dto.progress.Moncard;
import com.woniuxy.yoga.course.client.dto.progress.Subcard;
import com.woniuxy.yoga.course.client.dto.user.UserInfoDTO;
import com.woniuxy.yoga.course.dao.model.OrdinaryCourseBook;
import com.woniuxy.yoga.course.dao.mapper.OrdinaryCourseBookMapper;
import com.woniuxy.yoga.course.model.dto.ordinary.CoachQueryOrdinaryCourseBookDTO;
import com.woniuxy.yoga.course.model.dto.ordinary.OrdinaryCourseByIdDTO;
import com.woniuxy.yoga.course.model.dto.ordinary.UserQueryOrdinaryCourseBookDTO;
import com.woniuxy.yoga.course.model.exception.CourseException;
import com.woniuxy.yoga.course.model.exception.CourseExceptionCode;
import com.woniuxy.yoga.course.model.param.ordinary.AddOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.model.param.ordinary.CancelOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.model.param.ordinary.CoachQueryOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.model.param.ordinary.UserQueryOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.service.service.OrdinaryCourseBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 普通课程预约表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class OrdinaryCourseBookServiceImpl extends ServiceImpl<OrdinaryCourseBookMapper, OrdinaryCourseBook> implements OrdinaryCourseBookService {

    @Autowired
    private OrdinaryCourseBookMapper ordinaryCourseBookMapper;

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private ProductClient productClient;

    @Autowired
    private ProgressClient progressClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private CoachClient coachClient;

    /**
     * 新增普通课程预约
     * @param param
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void addOrdinaryCourseBook(AddOrdinaryCourseBookParam param) {
        //通过用户ID查询出所有普通预约
        QueryWrapper<OrdinaryCourseBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",param.getUserId());
        //预约状态不等于已取消
        wrapper.notIn("reservation_state",CourseState.ORDINARY_COURSE_RESERVATION_STATE_CANCELED);
        //获取数据
        List<OrdinaryCourseBook> ordinaryCourseBooks = ordinaryCourseBookMapper.selectList(wrapper);
        //判断是否存在
        if (ordinaryCourseBooks!=null&&ordinaryCourseBooks.size()>0){
            //遍历判断是否已经预约过
            for (OrdinaryCourseBook ordinaryCourseBook : ordinaryCourseBooks) {
                if (param.getCourseId()==ordinaryCourseBook.getCourseId()) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_NOT_REPEAT);
            }
        }
        //判断产品类型是否报错
        Result<ProductTypeByIdDTO> productTypeByIdDTOResult = productClient.getProductTypeById(param.getProductTypeId());
        if (productTypeByIdDTOResult.getCode()!=200) throw new CourseException(productTypeByIdDTOResult.getCode(),productTypeByIdDTOResult.getMessage());
        //获取数据
        ProductTypeByIdDTO productTypeByIdDTO = productTypeByIdDTOResult.getData();
        //判断是否存在
        if (productTypeByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_PRODUCT_TYPE_NOT_EXIST);
        //判断状态是否异常
        if (productTypeByIdDTO.getProductTypeState()!= ProductState.PRODUCT_TYPE_STATE_NORMAL) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_PRODUCT_TYPE_STATE_ERROR);
        //根据产品类型判断是什么产品类型 调用对应的进度方法
        if (param.getProductTypeId()==1){
            //体验卡 判断用户是否拥有体验卡
            Result<Expcard> expcardResult = progressClient.selectExpcardUnfinishExp(param.getUserId());
            if (expcardResult.getCode()!=200) throw new CourseException(expcardResult.getCode(),expcardResult.getMessage());
            //获取数据
            Expcard expcard = expcardResult.getData();
            //判断体验卡是否存在
            if (expcard==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_EXPCARD_NOT_EXIST);
            //先判断该卡是否和该课程是同一家门店
            //拥有 体验卡完成
            Result<?> result = progressClient.finishExpcard(expcard.getExpcardId());
            if (result.getCode()!=200) throw new CourseException(result.getCode(),result.getMessage());
        }else if (param.getProductTypeId()==2){
            //次卡 判断用户是否拥有次卡
            Result<List<Subcard>> subcardResult = progressClient.getSubcardUnfinishSub(param.getUserId());
            if (subcardResult.getCode()!=200) throw new CourseException(subcardResult.getCode(),subcardResult.getMessage());
            //获取数据
            List<Subcard> subcards = subcardResult.getData();
            //判断是否存在
            if (subcards==null||subcards.size()<1) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_SUBCARD_NOT_EXIST);
            //拿到第一次未完成的次卡 拥有 次数加一
            Result<String> stringResult = progressClient.addSubCardCount(subcards.get(0).getSubcardId());
            if (stringResult.getCode()!=200) throw new CourseException(stringResult.getCode(),stringResult.getMessage());
        } else if (param.getProductTypeId()==3) {
            //月卡 判断用户是否拥有月卡 判断预约的时间是否在月卡时间之内
            Result<List<Moncard>> moncardResult = progressClient.getMoncardUnfinishMon(param.getUserId());
            if (moncardResult.getCode()!=200) throw new CourseException(moncardResult.getCode(),moncardResult.getMessage());
            //获取数据
            List<Moncard> moncards = moncardResult.getData();
            //判断是否存在
            if (moncards==null||moncards.size()<1) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_MONCARD_NOT_EXIST);
            //拥有 判断是否只有一条数据
            if (moncards.size()==1){
                //获取唯一月卡时间段
                Moncard moncard = moncards.get(0);
                //获取开始时间
                String moncardStartTime = moncard.getMoncardStartTime();
                long startTime = DateUtil.convertToMilliseconds(moncardStartTime);
                //获取结束时间
                String moncardEndTime = moncard.getMoncardEndTime();
                long endTime = DateUtil.convertToMilliseconds(moncardEndTime);
                //获取当前时间
                long currentTime = new Date().getTime();
                //判断当前时间是否在月卡有效期之内
                if (currentTime<startTime || currentTime>endTime) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_MONCARD_TIME_EXPIRED);
            }else {
                //用于保存最早时间的对象
                Moncard moncard = null;
                //存在多个月卡 双重循环 获取最早时间的月卡
                for (int i = 0; i < moncards.size(); i++) {
                    //打标记 默认是最前面的时间
                    boolean mark = true;
                    for (int j = 0; j < moncards.size(); j++) {
                        //排除和自己相同的月卡
                        if (moncards.get(i).getMoncardId()==moncards.get(j).getMoncardId()) continue;
                        //不相同 判断时间先后顺序 只要外层循环的结束时间存在大于内层循环的开始时间就判断下一次
                        //获取外层循环结束时间
                        String moncardEndTime = moncards.get(i).getMoncardEndTime();
                        long endTime = DateUtil.convertToMilliseconds(moncardEndTime);
                        //获取内层循环开始时间
                        String moncardStartTime = moncards.get(j).getMoncardStartTime();
                        long startTime = DateUtil.convertToMilliseconds(moncardStartTime);
                        if (endTime>=startTime){
                            //改变标记
                            mark = false;
                            break;
                        }
                    }
                    //内层循环完成 判断标记是否改变
                    if (mark){
                        moncard = moncards.get(i);
                        break;
                    }
                }
                //拿到获取的最早月卡判断时间是否过期
                if (moncard!=null){
                    //获取开始时间
                    String moncardStartTime = moncard.getMoncardStartTime();
                    long startTime = DateUtil.convertToMilliseconds(moncardStartTime);
                    //获取结束时间
                    String moncardEndTime = moncard.getMoncardEndTime();
                    long endTime = DateUtil.convertToMilliseconds(moncardEndTime);
                    //获取当前时间
                    long currentTime = new Date().getTime();
                    //判断当前时间是否在月卡有效期之内
                    if (currentTime<startTime || currentTime>endTime) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_MONCARD_TIME_EXPIRED);
                }
            }
        } else if (param.getProductTypeId()==4) {
            //终身卡 判断用户是否拥有终身卡
            Result<Lifecard> lifecardResult = progressClient.getLifecardByUserId(param.getUserId());
            if (lifecardResult.getCode()!=200) throw new CourseException(lifecardResult.getCode(),lifecardResult.getMessage());
            //获取数据
            Lifecard lifecard = lifecardResult.getData();
            //判断是否存在
            if (lifecard==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_LIFECARD_NOT_EXIST);
            //拥有 终身卡次数加一
            Result<String> stringResult = progressClient.addLifeCardCount(lifecard.getLifecardId());
            if (stringResult.getCode()!=200) throw new CourseException(stringResult.getCode(),stringResult.getMessage());
        }
        //判断普通课程是否报错
        Result<OrdinaryCourseByIdDTO> ordinaryCourseByIdDTOResult = courseClient.getOrdinaryCourseById(param.getCourseId());
        if (ordinaryCourseByIdDTOResult.getCode()!=200) throw new CourseException(ordinaryCourseByIdDTOResult.getCode(),ordinaryCourseByIdDTOResult.getMessage());
        //获取数据
        OrdinaryCourseByIdDTO ordinaryCourseByIdDTO = ordinaryCourseByIdDTOResult.getData();
        //判断是否存在
        if (ordinaryCourseByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_COURSE_NOT_EXIST);
        //判断普通课程状态是否异常
        if (ordinaryCourseByIdDTO.getCourseState()!=CourseState.ORDINARY_COURSE_STATE_WAIT) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_COURSE_STATE_ERROR);
        //获取课程中的开课时间
        String courseStartDate = ordinaryCourseByIdDTO.getCourseStartDate();
        long courseStartTime = DateUtil.convertToMilliseconds(courseStartDate);
        //获取当前时间
        long currentTime = new Date().getTime();
        //判断预约时间是否是提前两个小时预约
        if (courseStartTime-currentTime<2*60*60*1000L) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ADVANCE_TWO_HOUR);
        //生成预约数据 对象转换
        OrdinaryCourseBook ordinaryCourseBook = BeanUtil.toBean(param, OrdinaryCourseBook.class);
        //赋值预约状态 已预约
        ordinaryCourseBook.setReservationState(CourseState.ORDINARY_COURSE_RESERVATION_STATE_BOOKED);
        //设置预约时间为当前时间 取消时间没有
        ordinaryCourseBook.setReservationTime(DateUtil.dateTime2String(new Date()));
        //课程类别id赋值
        ordinaryCourseBook.setCourseTypeId(1);
        //课程类别赋值
        ordinaryCourseBook.setCourseTypeName("普通课程");
        //新增数据
        ordinaryCourseBookMapper.insert(ordinaryCourseBook);
        //调用普通课程人数加一 判断是否达到最大人数
        Result<?> result = courseClient.OrdinaryCourseIncreaseCurrentCount(ordinaryCourseByIdDTO.getCourseId());
        if (result.getCode()!=200) throw new CourseException(result.getCode(),result.getMessage());
    }

    /**
     * 预约取消
     * @param param
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void cancelOrdinaryCourseBook(CancelOrdinaryCourseBookParam param) {
        //获取普通预约数据
        OrdinaryCourseBook ordinaryCourseBook = ordinaryCourseBookMapper.selectById(param.getReservationId());
        //判断是否存在
        if (ordinaryCourseBook==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_RESERVATION_NOT_EXIST);
        //判断普通预约状态是否异常
        if (ordinaryCourseBook.getReservationState()!=CourseState.ORDINARY_COURSE_RESERVATION_STATE_BOOKED) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_RESERVATION_STATE_ERROR);
        //判断普通课程是否报错
        Result<OrdinaryCourseByIdDTO> courseByIdDTOResult = courseClient.getOrdinaryCourseById(param.getCourseId());
        if (courseByIdDTOResult.getCode()!=200) throw new CourseException(courseByIdDTOResult.getCode(),courseByIdDTOResult.getMessage());
        //获取数据
        OrdinaryCourseByIdDTO ordinaryCourseByIdDTO = courseByIdDTOResult.getData();
        //判断是否存在
        if (ordinaryCourseByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_COURSE_NOT_EXIST);
        //判断普通课程状态是否异常
        if (ordinaryCourseByIdDTO.getCourseState()!=CourseState.ORDINARY_COURSE_STATE_WAIT) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_COURSE_STATE_ERROR);
        //普通课程已预约人数减一
        Result<?> result = courseClient.OrdinaryCourseReduceCurrentCount(ordinaryCourseByIdDTO.getCourseId());
        if (result.getCode()!=200) throw new CourseException(result.getCode(),result.getMessage());
        //判断产品类型是否报错
        Result<ProductTypeByIdDTO> productTypeByIdDTOResult = productClient.getProductTypeById(param.getProductTypeId());
        if (productTypeByIdDTOResult.getCode()!=200) throw new CourseException(productTypeByIdDTOResult.getCode(),productTypeByIdDTOResult.getMessage());
        //获取数据
        ProductTypeByIdDTO productTypeByIdDTO = productTypeByIdDTOResult.getData();
        //判断是否存在
        if (productTypeByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_PRODUCT_TYPE_NOT_EXIST);
        //判断产品类型状态是否异常
        if (productTypeByIdDTO.getProductTypeState()!=ProductState.PRODUCT_TYPE_STATE_NORMAL) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_PRODUCT_TYPE_STATE_ERROR);
        //判断产品类型属于哪一种 月卡不管
        if (param.getProductTypeId()==0){
            //体验卡 不能取消
            throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_EXPCARD_USE_NOT_CANCEL);
        }else if (param.getProductTypeId()==1){
            //次卡 查询是哪一张次卡
            Result<List<Subcard>> subcardResult = progressClient.getSubcardUnfinishSub(param.getUserId());
            if (subcardResult.getCode()!=200) throw new CourseException(subcardResult.getCode(),subcardResult.getMessage());
            //获取数据
            List<Subcard> subcards = subcardResult.getData();
            //判断是否存在
            if (subcards==null||subcards.size()<1) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_SUBCARD_NOT_EXIST);
            //获取第一个次卡
            Subcard subcard = subcards.get(0);
            //次卡 取消次数
            Result<String> stringResult = progressClient.reduceSubCardCount(subcard.getSubcardId());
            if (stringResult.getCode()!=200) throw new CourseException(stringResult.getCode(),stringResult.getMessage());
        }else if (param.getProductTypeId()==3){
            //终身卡 查询
            Result<Lifecard> lifecardResult = progressClient.getLifecardByUserId(param.getUserId());
            if (lifecardResult.getCode()!=200) throw new CourseException(lifecardResult.getCode(),lifecardResult.getMessage());
            //获取数据
            Lifecard lifecard = lifecardResult.getData();
            //判断是否存在
            if (lifecard==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_LIFECARD_NOT_EXIST);
            //次数减一
            Result<String> stringResult = progressClient.reduceLifeCardCount(lifecard.getLifecardId());
            if (stringResult.getCode()!=200) throw new CourseException(stringResult.getCode(),stringResult.getMessage());
        }
        //普通预约状态修改成取消
        ordinaryCourseBook.setReservationState(CourseState.ORDINARY_COURSE_RESERVATION_STATE_CANCELED);
        ordinaryCourseBookMapper.updateById(ordinaryCourseBook);
    }

    /**
     * 预约签到
     * @param reservationId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void signedOrdinaryCourseBook(Integer reservationId) {
        OrdinaryCourseBook ordinaryCourseBook = ordinaryCourseBookMapper.selectById(reservationId);
        //判断是否存在
        if (ordinaryCourseBook==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_RESERVATION_NOT_EXIST);
        //判断状态是否是已预约状态
        if (ordinaryCourseBook.getReservationState()!=CourseState.ORDINARY_COURSE_RESERVATION_STATE_BOOKED) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_ORDINARY_RESERVATION_STATE_ERROR);
        //修改成签到
        ordinaryCourseBook.setReservationState(CourseState.ORDINARY_COURSE_RESERVATION_STATE_SIGNED);
        ordinaryCourseBookMapper.updateById(ordinaryCourseBook);
    }

    /**
     * 教练查询当前课程的普通预约信息
     * @param param
     * @return
     */
    @Override
    public PageInfo coachQueryOrdinaryCourseBook(CoachQueryOrdinaryCourseBookParam param) {
        QueryWrapper<OrdinaryCourseBook> wrapper = new QueryWrapper<>();
        //预约状态搜索
        if (ObjectUtil.isNotEmpty(param.getReservationState())){
            wrapper.eq("reservation_state",param.getReservationState());
        }
        //索引排序
        wrapper.orderByAsc("reservation_id");
        //分页查询
        Page<OrdinaryCourseBook> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 10 : param.getPageSize());
        ordinaryCourseBookMapper.selectPage(page,wrapper);
        //集合转换
        List<CoachQueryOrdinaryCourseBookDTO> coachQueryOrdinaryCourseBookDTOS = new ArrayList<>();
        //获取课程
        Result<OrdinaryCourseByIdDTO> ordinaryCourseByIdDTOResult = courseClient.getOrdinaryCourseById(param.getCourseId());
        if (ordinaryCourseByIdDTOResult.getCode()!=200) throw new CourseException(ordinaryCourseByIdDTOResult.getCode(),ordinaryCourseByIdDTOResult.getMessage());
        OrdinaryCourseByIdDTO ordinaryCourseByIdDTO = ordinaryCourseByIdDTOResult.getData();
        //获取教练
        Result<Coach> coachResult = coachClient.getCoachById(param.getCoachId());
        if (coachResult.getCode()!=200) throw new CourseException(coachResult.getCode(),coachResult.getMessage());
        Coach coach = coachResult.getData();
        //当前课程属于教练 并且教练的门店和课程门店相同
        if (ordinaryCourseByIdDTO!=null&&coach!=null){
            if (ordinaryCourseByIdDTO.getCoachId()==coach.getCoachId()&&ordinaryCourseByIdDTO.getStoreId()==coach.getStoreId()){
                if (page.getRecords()!=null&&page.getRecords().size()>0){
                    for (OrdinaryCourseBook ordinaryCourseBook : page.getRecords()) {
                        //判断预约信息是不是当前课程的
                        if (ordinaryCourseBook.getCourseId()==param.getCourseId()){
                            //对象转换
                            CoachQueryOrdinaryCourseBookDTO coachQueryOrdinaryCourseBookDTO = BeanUtil.toBean(ordinaryCourseBook, CoachQueryOrdinaryCourseBookDTO.class);
                            //用户名称赋值
                            Result<UserInfoDTO> userInfoDTOResult = userClient.getUserByUserId(ordinaryCourseBook.getUserId());
                            if (userInfoDTOResult.getCode()!=200) throw new CourseException(userInfoDTOResult.getCode(),userInfoDTOResult.getMessage());
                            //获取数据
                            UserInfoDTO userInfoDTO = userInfoDTOResult.getData();
                            //判断是否存在
                            if (userInfoDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_USER_INFO_NOT_EXIST);
                            coachQueryOrdinaryCourseBookDTO.setUserName(userInfoDTO.getUserName());
                            //状态赋值
                            coachQueryOrdinaryCourseBookDTO.setReservationStateName(ordinaryCourseBook.getReservationState()==0?"已预约":ordinaryCourseBook.getReservationState()==1?"已取消":"已签到");
                            //保存到集合中
                            coachQueryOrdinaryCourseBookDTOS.add(coachQueryOrdinaryCourseBookDTO);
                        }
                    }
                }
            }
        }
        //分页对象转换
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(coachQueryOrdinaryCourseBookDTOS);
        return pageInfo;
    }

    /**
     * 学员查询自己的所有普通预约信息
     * @param param
     * @return
     */
    @Override
    public PageInfo userQueryOrdinaryCourseBook(UserQueryOrdinaryCourseBookParam param) {
        QueryWrapper<OrdinaryCourseBook> wrapper = new QueryWrapper<>();
        //用户ID
        wrapper.eq("user_id",param.getUserId());
        //预约状态搜索
        if (ObjectUtil.isNotEmpty(param.getReservationState())){
            wrapper.eq("reservation_state",param.getReservationState());
        }
        //索引排序
        wrapper.orderByAsc("reservation_id");
        //分页查询
        Page<OrdinaryCourseBook> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 10 : param.getPageSize());
        ordinaryCourseBookMapper.selectPage(page,wrapper);
        //集合转换
        List<UserQueryOrdinaryCourseBookDTO> userQueryOrdinaryCourseBookDTOS = new ArrayList<>();
        if (page.getRecords()!=null&&page.getRecords().size()>0){
            for (OrdinaryCourseBook ordinaryCourseBook : page.getRecords()) {
                //对象转换
                UserQueryOrdinaryCourseBookDTO userQueryOrdinaryCourseBookDTO = BeanUtil.toBean(ordinaryCourseBook, UserQueryOrdinaryCourseBookDTO.class);
                //用户名称赋值
                Result<UserInfoDTO> userInfoDTOResult = userClient.getUserByUserId(ordinaryCourseBook.getUserId());
                if (userInfoDTOResult.getCode()!=200) throw new CourseException(userInfoDTOResult.getCode(),userInfoDTOResult.getMessage());
                //获取数据
                UserInfoDTO userInfoDTO = userInfoDTOResult.getData();
                //判断是否存在
                if (userInfoDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_USER_INFO_NOT_EXIST);
                userQueryOrdinaryCourseBookDTO.setUserName(userInfoDTO.getUserName());
                //状态赋值
                userQueryOrdinaryCourseBookDTO.setReservationStateName(ordinaryCourseBook.getReservationState()==0?"已预约":ordinaryCourseBook.getReservationState()==1?"已取消":"已签到");
                //保存到集合中
                userQueryOrdinaryCourseBookDTOS.add(userQueryOrdinaryCourseBookDTO);
            }
        }
        //分页对象转换
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(userQueryOrdinaryCourseBookDTOS);
        return pageInfo;
    }
}
